<template>
  <div>
    <el-dialog
      width="600px"
      :title="$t('创建取货单')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="线下店铺" prop="shopId">
          <el-select v-model="form.shopId" :placeholder="$t('请输入')" clearable style="width: 100%">
            <el-option v-for="shop in shopList" :key="shop.id" :label="shop.name" :value="shop.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="取货人" prop="receiveName">
          <el-select @change="handleReceiveName" v-model="form.receiveName" clearable style="width: 100%">
            <el-option v-for="shop in receiveNameList" :key="shop.id" :label="shop.receiveName" :value="shop.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="receivePhone">
          <el-input v-model="form.receivePhone" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item label="邮编" prop="receivePostcode">
          <el-input v-model="form.receivePostcode" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item label="地址1" prop="address1">
          <el-select filterable v-model="form.address1" clearable style="width: 100%">
            <el-option v-for="item in address1List" :key="item.id" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="地址2" prop="address2">
          <el-input v-model="form.address2" :placeholder="$t('请输入')" />
        </el-form-item>
        <el-form-item label="地址3" prop="address3">
          <el-input v-model="form.address3" :placeholder="$t('请输入')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCreate" type="primary">{{ $t('创建') }}</el-button>
        <el-button @click="cancel">{{ $t('关闭') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { skuSellerOfflineAdd, getOrderClientList, getDistributorShopList } from '@/api/orderManage/skuSellerOffline'
import { address1List } from '@/api/platform/sellerManage'

export default {
  activated() {
    this.$nextTick(() => this.$refs['mianRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      shopList: [],
      receiveNameList: [],
      address1List: [],
      // 表单参数
      form: {
        shopId: '',
        receiveName: '',
        receivePhone: '',
        receivePostcode: '',
        address1: '',
        address2: '',
        address3: '',
      },
      // 表单校验
      rules: {
        shopId: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        receiveName: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        // receivePhone: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        // receivePostcode: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        address1: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        // address2: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
        // address3: [{ required: true, message: this.$t('请输入'), trigger: 'blur' }],
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
        this.getList()
      }
    },
  },
  methods: {
    handleReceiveName(id) {
      const data = this.receiveNameList.filter((i) => i.id == id)[0]
      if (data) {
        this.form.address1 = data.address1
        this.form.address2 = data.address2
        this.form.address3 = data.address3
        this.form.receivePhone = data.receivePhone
        this.form.receivePostcode = data.receivePostcode
        this.form._id = id
        this.form._receiveName = data.receiveName
      }
    },
    /** 线下店铺 */
    getList() {
      getDistributorShopList({ platformType: 9 }).then((res) => {
        this.shopList = res.rows
      })
      getOrderClientList().then((res) => {
        this.receiveNameList = res.data
      })
      address1List().then((res) => {
        this.address1List = res.data
      })
    },

    handleCreate() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const data = {
            ...this.form,
            clientId: this.form._id,
            receiveName: this.form._receiveName,
          }
          skuSellerOfflineAdd(data).then((res) => {
            const { code, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg })
              this.$emit('getList')
              this.cancel()
            }
          })
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
